package com.kaishengit.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.kaishengit.dao.core.BaseDao;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.User;

@Repository
public class ContactDao extends BaseDao<Contact, Integer> {

	@SuppressWarnings("unchecked")
	public List<Contact> findByQ(String q) {
		Criteria cri = getSession().createCriteria(Contact.class);
		cri.add(Restrictions.or(Restrictions.eq("username", q), Restrictions.or(Restrictions.eq("pinyin", q), Restrictions.eq("abbr", q))));
		return cri.list();
	}

	@SuppressWarnings("unchecked")
	public List<Contact> findByUserViews(User user) {
		Criteria cri = getSession().createCriteria(Contact.class);
		cri.add(Restrictions.or(Restrictions.eq("views", "all"), Restrictions.eq("views", user.getUsername())));
		//cri.add(Restrictions.eq("views", "all"));
		return cri.list();
	}

	
	public Contact findByp(String propoty,String value) {
		Criteria cri = getSession().createCriteria(Contact.class);
		cri.add(Restrictions.eq(propoty,value));
		return (Contact) cri.uniqueResult();
	}

	public Contact findContact(Integer id) {
		Criteria cri = getSession().createCriteria(Contact.class);
		cri.add(Restrictions.eq("id", id));
		return (Contact) cri.uniqueResult();
	}

}
